﻿/*===================================================
* 类名称: ModuleManager
* 类描述:
* 创建人: oream
* 创建时间: 2017/12/23 21:49:51
* 修改人: 
* 修改时间:
* 版本： @version 1.0
=====================================================*/

using System;
using System.Collections.Generic;
using System.Reflection;
using Model;
using UnityEngine;

namespace Hotfix
{
    public class ModuleManager : Singleton<ModuleManager>
    {
        Dictionary<Type, Module> dicGetModel;

        /// <summary>
        /// 初始化
        /// </summary>
        public void InitStart()
        {
            dicGetModel = new Dictionary<Type, Module>();
        }

        /// <summary>
        /// 添加Module
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="module"></param>
        public void AddModule<T>(T module) where T : Module
        {
            T moduleObject = Activator.CreateInstance<T>();
            if (moduleObject == null) Debug.LogError("ModuleManager::AddModule() 加载moduleObject错误");
            dicGetModel[moduleObject.GetType()] = moduleObject as Module;
        }

        /// <summary>
        /// 返回Module
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetModule<T>() where T : Module
        {
            if (!dicGetModel.TryGetValue(typeof(T), out Module module))
            {
                Debug.LogError("需要返回的类型是错误了");
                return default(T);
            }
            T obj = module as T;
            return obj;
        }

        /// <summary>
        /// 将该Module删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public void Disposable<T>() where T : Module
        {
            T t = GetModule<T>();
            t.Dispose();
            dicGetModel.Remove(typeof(T));
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="message"></param>
        /// <param name="_params"></param>
        public void SendMessage<T>(string message, params object[] _params) where T : Module
        {
            T t = GetModule<T>();
            MethodInfo function = t.GetType().GetMethod(message);
            function.Invoke(t, _params);

        }

        Dictionary<string, Action<object>> dicEventListenFroms = new Dictionary<string, Action<object>>();


        Dictionary<string, object> dicEventBufferObjectValue = new Dictionary<string, object>();

        /// <summary>
        /// 添加事件监听
        /// </summary>
        public void AddEventListen(string eventName, Action<object> action)
        {
            if (dicEventBufferObjectValue.ContainsKey(eventName))
            {
                action(dicEventBufferObjectValue[eventName]);
                dicEventBufferObjectValue.Remove(eventName);
            }
            if (!dicEventListenFroms.TryGetValue(eventName, out Action<object> _event))
            {
                dicEventListenFroms[eventName] = action;
                return;
            }
            _event += action;
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="eventName"></param>
        /// <param name="_object"></param>
        public void SendEvent(string eventName, object _object)
        {
            if (!dicEventListenFroms.ContainsKey(eventName))
            {
                if (!dicEventBufferObjectValue.TryGetValue(eventName, out object obj))
                {
                    dicEventBufferObjectValue.Add(eventName, _object);
                }
                dicEventBufferObjectValue[eventName] = _object;
            }
            dicEventListenFroms[eventName](_object);
        }


    }
}
